package com.itboye.ihomebank.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.HaveInBean;
import com.itboye.ihomebank.activity.finance.InvestmentListActivity;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.itboye.ihomebank.R.id.touzi_zhuanrang;

/**
 * 我的投资(进行中)
 */
public class HaveInhaAdapter extends BaseAdapter implements View.OnClickListener{
    List<HaveInBean.InfoList> lists;
    Context context;
    String status;

    ClickListener listOnclik;

    public HaveInhaAdapter(List<HaveInBean.InfoList> lists, Context context,ClickListener listOnclik) {
        this.lists = lists;
        this.context = context;
        this.listOnclik=listOnclik;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHouder holder;
        if (convertView == null) {
            holder = new ViewHouder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_havein_fragment, null);
            holder.touzi_title = (TextView) convertView.findViewById(R.id.touzi_title);//.getView(R.id.tvNumber);
            holder.touzi_biaoqian = (TextView) convertView.findViewById(R.id.touzi_biaoqian);
            holder.touzi_baifen = (TextView) convertView.findViewById(R.id.touzi_baifen);
            holder.touzi_yue = (TextView) convertView.findViewById(R.id.touzi_yue);
            holder.touzi_name = (TextView) convertView.findViewById(R.id.touzi_name);
            holder.touzi_shengyu = (TextView) convertView.findViewById(R.id.touzi_shengyu);
            holder.touzi_bai = (TextView) convertView.findViewById(R.id.touzi_bai);
            holder.seekbar = (ProgressBar) convertView.findViewById(R.id.seekbar);
            //时间
            holder.linTimeData = (LinearLayout) convertView.findViewById(R.id.linTimeData);
            holder.tvQishiTimeData = (TextView) convertView.findViewById(R.id.tvQishiTimeData);
            holder.tvShouCiTimeData = (TextView) convertView.findViewById(R.id.tvShouCiTimeData);
            holder.tvJieSuTimeData = (TextView) convertView.findViewById(R.id.tvJieSuTimeData);
            holder.touzi_jiahua = (TextView) convertView.findViewById(R.id.touzi_jiahua);
            holder.linearHavein = (LinearLayout) convertView.findViewById(R.id.linearHavein);
            holder.touzi_zhuanrang= (TextView) convertView.findViewById(touzi_zhuanrang);
            convertView.setTag(holder);
        } else {
            holder = (ViewHouder) convertView.getTag();
        }


        DecimalFormat df = new DecimalFormat("######0.00");
        //剩余
        Double shengyue = Double.parseDouble(lists.get(position).getRemaining_amount());
        String tvshengyue = df.format(shengyue / 100);

//进度条
        Double jindu = Double.parseDouble(lists.get(position).getTotal_money());
        Double jin = Double.parseDouble(lists.get(position).getRemaining_amount());
        Double jingtutiao = jindu - jin;
        Double douNumber = jingtutiao / jindu;
//        /        /进度条
        DecimalFormat dfs = new DecimalFormat("######0");
        Double x=douNumber*1000;
        int i=Integer.parseInt(dfs.format(x));
        holder.seekbar.setMax(1000);
        holder.seekbar.setProgress(i);
        Log.d("naerwererew", douNumber * 1000 + "aa"+i);
        //期望率
        String baidufenbi =lists.get(position).getExpected_annualize();
        Double doubaidfen = Double.parseDouble(baidufenbi);
        String tvBaifenbao = df.format(doubaidfen / 100);


        holder.touzi_title.setText(lists.get(position).getTitle());
        holder.touzi_biaoqian.setText(lists.get(position).getDt_type_name());
        holder.touzi_baifen.setText(tvBaifenbao + "%");
        holder.touzi_name.setText(lists.get(position).getDt_repayment_type_name());
        holder.touzi_shengyu.setText(tvshengyue);


       String namemath=lists.get(position).getProject_duration_month();


        if(!namemath.equals("0")){
            holder.touzi_yue.setText(namemath+ "个月");
        }else{
            holder.touzi_yue.setText(lists.get(position).getProject_duration_day() + "天");
        }
        holder.touzi_bai.setText(douNumber * 100 + "%");

        holder.tvQishiTimeData.setText(lists.get(position).getInterest_start_time());//开始还款
        holder.tvShouCiTimeData.setText(lists.get(position).getInterest_end_time());//下期还款
        holder.tvJieSuTimeData.setText(lists.get(position).getNext_interest_time());//最后还款
        holder.touzi_jiahua.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, InvestmentListActivity.class);
                intent.putExtra("rate_year", lists.get(position).getExpected_annualize());
                intent.putExtra("money", lists.get(position).getTotal_money());
                intent.putExtra("month", lists.get(position).getProject_duration_month());
                intent.putExtra("day", lists.get(position).getProject_duration_day());
                intent.putExtra("pay_type", lists.get(position).getDt_repayment_type());
                if (isDateStringValid(lists.get(position).getProject_duration_month())) {
                    if (lists.get(position).getProject_duration_month().length() == 10) {
                        intent.putExtra("start_time", lists.get(position).getProject_duration_month());
                    } else {
                        Log.d("不是日期", "不是日期格式"+onSimleData());
                        intent.putExtra("start_time", onSimleData());

                    }
                } else {
                    intent.putExtra("start_time", onSimleData());
                    Log.d("不是日期", "不是日期格式"+onSimleData());
                }

                context.startActivity(intent);
            }
        });


        status = lists.get(position).getStatus();
        onStatus(status, holder,position);
        holder.touzi_zhuanrang.setOnClickListener(this);
        holder.touzi_zhuanrang.setTag(position);

        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.touzi_zhuanrang:
                listOnclik.onCle(v,context);
                break;
        }
    }

    public void onStatus(String status, ViewHouder houder,int position) {
        if (status.equals("0")) {
            houder.linTimeData.setVisibility(View.GONE);

        } else if (status.equals("1")) {
            houder.linTimeData.setVisibility(View.GONE);
        } else if (status.equals("2")) {
            houder.linTimeData.setVisibility(View.VISIBLE);
            if(lists.get(position).getCan_transfer().equals("1")){
                houder.touzi_zhuanrang.setVisibility(View.VISIBLE);
            }else{
                houder.touzi_zhuanrang.setVisibility(View.INVISIBLE);
            }
        } else if (status.equals("3")) {
            houder.linTimeData.setVisibility(View.GONE);
            ///为了回款完成设置
            houder.touzi_biaoqian.setTextColor(context.getResources().getColor(R.color.gray_one));
            houder.touzi_title.setTextColor(context.getResources().getColor(R.color.gray_one));
            houder.touzi_baifen.setTextColor(context.getResources().getColor(R.color.gray_one));
            houder.touzi_yue.setTextColor(context.getResources().getColor(R.color.gray_one));
            houder.touzi_shengyu.setTextColor(context.getResources().getColor(R.color.gray_one));
            houder.tvQishiTimeData.setTextColor(context.getResources().getColor(R.color.gray_one));
            houder.tvShouCiTimeData.setTextColor(context.getResources().getColor(R.color.gray_one));
            houder.tvJieSuTimeData.setTextColor(context.getResources().getColor(R.color.gray_one));
            houder.linearHavein.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.image_3));
        }
    }

    /**
     * 判断字符串是否为日期格式
     * @param date
     * @return
     */
    public boolean isDateStringValid(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 输入对象不为空
        try {
            sdf.parse(date);
            return true;
        } catch (java.text.ParseException e) {
            return false;
        }
    }

    /**
     * 得到系统时间
     * @return
     */
    public String onSimleData() {
        String str;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        str = formatter.format(curDate);
        return str;
    }

    public class ViewHouder {
        private TextView touzi_name,
                touzi_bai;
        private ProgressBar seekbar;
        //时间
        private LinearLayout linTimeData;
        private LinearLayout linearHavein;
        private TextView tvQishiTimeData;
        private TextView tvShouCiTimeData;
        private TextView tvJieSuTimeData;
        private TextView touzi_jiahua;

        TextView touzi_zhuanrang;//转让

        ///为了回款完成设置
        TextView touzi_biaoqian;//biaoti
        TextView touzi_title;
        private TextView touzi_baifen, touzi_yue, touzi_shengyu;
    }
    public interface ClickListener{
        public void onCle(View v,Context context);//转让
    }

}
